<?php
	/**
	 * 布局管理
	 * 界面布局渲染层
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	require_once 'base.php';

	class layout extends base
	{

		/**
		 * 返回页面内容
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function page( $tpl,$data )
		{
			$layout = $this->get_layout( $data );
			$output = $this->get_tpl( $tpl, $data, $layout );

			return $output;
		}

		function get_layout( $data )
		{
			$layout = array();
			$layout['layout_header'] = $this->layout_header( $data );
			$layout['layout_footer'] = $this->layout_footer( $data );

			return $layout;
		}

		function layout_header( $data=array() )
		{
			return $this->get_tpl('header', $data);
		}

		function layout_footer( $data=array() )
		{
			return $this->get_tpl('footer', $data);
		}

		public function get_tpl( $tpl='', $data=array(), $layout=array() )
		{
			if( strpos($tpl, '.') > 0 )
			{
				$tpl = str_replace( '.', DS, $tpl);
			}

			$tpl = $this->_config[ 'dir' ][ 'tpl' ] . $tpl . '.php';

			if( is_file($tpl) )
			{
				$_config = $this->_config;

				if( is_array( $data ) )
				{
					extract( $data, EXTR_OVERWRITE );
				}
				if( is_array( $layout ) )
				{
					extract( $layout, EXTR_OVERWRITE );
				}

				ob_start();
				require_once $tpl;
				$content = ob_get_contents();
				ob_end_clean();

				return $content;
			}
			else
			{
				trigger_error('Template file ' . $tpl . ' not exists.', E_USER_ERROR);
			}
		}

	}
?>